//
//  NSString+ZZSize.m
//  ZhiYiBao
//
//  Created by athena on 2017/2/23.
//  Copyright © 2017年 athena. All rights reserved.
//

#import "NSString+ZZSize.h"
#define sdklive [[[UIDevice currentDevice] systemVersion] floatValue]

@implementation NSString (ZZSize)
+(CGSize)getSizeToString:(NSString *)string forFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode{
    CGSize textSize;
    if (sdklive<7.1) {
        textSize=[string sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
    }else{
        if (CGSizeEqualToSize(size, CGSizeZero))
        {
            NSMutableDictionary *stringStyle=[[NSMutableDictionary alloc]init];
            [stringStyle setObject:font forKey:NSFontAttributeName];
            NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
            textSize = [string sizeWithAttributes:attributes];
        }
        else
        {
            NSStringDrawingOptions option = NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
            //NSStringDrawingTruncatesLastVisibleLine如果文本内容超出指定的矩形限制，文本将被截去并在最后一个字符后加上省略号。 如果指定了NSStringDrawingUsesLineFragmentOrigin选项，则该选项被忽略 NSStringDrawingUsesFontLeading计算行高时使用行间距。（译者注：字体大小+行间距=行高）
            NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
            CGRect rect = [string boundingRectWithSize:size
                                               options:option
                                            attributes:attributes
                                               context:nil];
            textSize = rect.size;
        }
    }
    return textSize;
}
@end
